﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using b7.Models;
using b7.ViewModels;

namespace b7.Controllers
{
    public class ShoppingCartController : Controller
    {
        b7Models db=new b7Models();
        //
        // GET: /ShoppingCartController /

        public ActionResult Index()
        {
             var cart = ShoppingCart.GetCart(this.HttpContext);
 
            // Set up our ViewModel
            var viewModel = new ShoppingCartViewModel
            {
                //MemberItems = cart.GetMemberItems,
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };
            // Return the view
            return View(viewModel);
        }
        public ActionResult NotLogon()
        {
            return View();
        }
        public ActionResult AddToCart(int id)
        {
            if (Request.IsAuthenticated)
            {
                // Retrieve the album from the database
                var addedSanpham = db.Sanphams.Single(sanpham => sanpham.SanphamId == id);

                // Add it to the shopping cart
                var cart = ShoppingCart.GetCart(this.HttpContext);

                cart.AddToCart(addedSanpham);

                // Go back to the main store page for more shopping
                return RedirectToAction("Index");
            }
            else
            {
                //ViewData["smg"] = "bạn chưa đăng nhập";
                return RedirectToAction("NotLogon");
            }
        }
        //
        // AJAX: /ShoppingCart/RemoveFromCart/5
        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            // Xóa Sản phẩm khỏi giỏ hàng
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Lấy tên sản phẩm để xuất ra trong thông báo xóa
            string sanphamName = db.Carts.Single(item => item.RecordId == id).Sanpham.Name;

            // Remove from cart
            int itemCount = cart.RemoveFromCart(id);

            // Display the confirmation message
            var results = new ShoppingCartRemoveViewModel
            {
                Message = Server.HtmlEncode(sanphamName) +" has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                DeleteId = id
            };
            return Json(results);
        }
        //
        // GET: /ShoppingCart/CartSummary
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();
            return PartialView("CartSummary");
        }
        public ActionResult ViewOrder()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Set up our ViewModel
            var viewModel = new ShoppingCartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };
            // Return the view
            return View(viewModel);
        }
        public ActionResult UpdateCart(int id)
        {
            ShoppingCartViewModel cart = new ShoppingCartViewModel();
            
            return View(cart);
        }

        [HttpPost]
        public ActionResult UpdateQuantity(int Id, int quantity)
        {
            //Lay san pham
            var ProductIncrease = db.Sanphams.Single(p => p.SanphamId == Id);
            // Lay Gio Hang
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.UpdateQuantity(ProductIncrease, quantity);
            return RedirectToAction("ViewOrder");
        }

        public ActionResult Delete(int id)
        {
            Cart cart = db.Carts.Find(id);
            return View(cart);
        }
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Cart cart = db.Carts.Find(id);
            db.Carts.Remove(cart);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Ok(Cart cart)
        {
            //if (ModelState.IsValid)
            //{
            //    List<Orders> ord = db.Members.Where(x => x.UserName.Equals(model.UserName)).ToList();
            //}
            return RedirectToAction("Index","Home");
        }

    }
}
